package com.example.activemq.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Producer {

    public static final String CONNECT_URL = "tcp://localhost:61616";

    public static void main(String[] args) throws Exception {

        ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(CONNECT_URL);

        Connection activeMQConnection = mqConnectionFactory.createConnection();

        activeMQConnection.start();

        Session session = activeMQConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Destination queue = session.createQueue("test");


        MessageProducer messageProducer = session.createProducer(queue);

        for (int i = 0; i < 10; i++) {
            TextMessage textMessage = session.createTextMessage("msg:" + i);
            messageProducer.send(textMessage);
        }
        //关闭资源
        messageProducer.close();
        session.close();
        activeMQConnection.close();
        System.out.println("消息发送完成...");
    }
}
